﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rappelz.GameServer
{
    public class BoxCollision
    {
        public BoxCollision(uint _rx1, uint _ry1, uint _rx2, uint _ry2)
        {
            this.bIsGlobal = false;

            if (_rx2 < _rx1)
            {
                this.left = (int)_rx2 - 5;
                this.right = (int)_rx1 + 5;
            }
            else
            {
                this.left = (int)_rx1 - 5;
                this.right = (int)_rx2 + 5;
            }
            if (_ry2 < _ry1)
            {
                this.top = (int)_ry2 - 5;
                this.bottom = (int)_ry1 + 5;
            }
            else
            {
                this.top = (int)_ry1 - 5;
                this.bottom = (int)_ry2 + 5;
            }
        }

        public BoxCollision(uint _rx1, uint _ry1, int range)
        {
            this.bIsGlobal = false;
            this.left = (int)(_rx1 - range - 2);
            this.right = (int)(_rx1 + range + 2);
            this.top = (int)(_ry1 - range - 2);
            this.bottom = (int)(_ry1 + range + 2);
        }

        public BoxCollision(uint _rx1, uint _ry1)
        {
            this.bIsGlobal = false;
            this.right = (int)(_rx1 + 5);
            this.left = (int)(_rx1 - 5);
            this.top = (int)(_ry1 - 5);
            this.bottom = (int)(_ry1 + 5);
        }

        public BoxCollision(float ax, float ay, float atx, float aty)
        {
            this.bIsGlobal = false;
            this.left = (int)(ax / GameContent.g_nRegionSize);
            this.top = (int)(ay / GameContent.g_nRegionSize);
            this.right = (int)(atx / GameContent.g_nRegionSize);
            this.bottom = (int)(aty / GameContent.g_nRegionSize);
        }

        public bool is_subset_of(BoxCollision c)
        {
            bool result = true;

            if (!c.bIsGlobal)
            {
                if (this.bIsGlobal || c.left > this.left || c.right < this.right || c.top > this.top || c.bottom < this.top)
                    result = false;
            }
            return result;
        }

        public bool is_exclusive(BoxCollision c)
        {
            int v1;
            int v2;

            v1 = c.right <= this.right ? c.right : this.right;
            v2 = this.left >= c.left ? this.left : c.left;
            if (v1 < v2)
                return false;

            v1 = c.bottom >= this.bottom ? this.bottom : c.bottom;
            v2 = this.top >= c.top ? this.top : c.top;

            if (v1 < v2)
                return false;

            return true;
        }


        public bool bIsGlobal;          // 0x0
        public int left;                // 0x4
        public int right;               // 0x8
        public int top;                 // 0xC
        public int bottom;              // 0x10

    }
}
